local BackgroundBG = {}

function BackgroundBG:new(xInit)
   local background = display.newGroup()

   background.trees1 = nil
   background.trees2 = nil
   background.trees3 = nil
   background.trees4 = nil
   background.trees5 = nil
   background.trees6 = nil
   background.mountains = nil
   background.thunderLight = nil
   background.thunderLightTimer = 8
   background.thunderPng = nil

   local w = X
   background.playerX = xInit

   function createTrees(x, y)
      local trees = display.newImageRect( "resources/sprites/treesBG.png", w, w*0.22 )
      trees.x = x
      trees.y = y
      return trees
   end

   function updateTrees(trees1, trees2, inc)
      local aux
      trees1.x = trees1.x + inc * 0.4
      trees2.x = trees2.x + inc * 0.4
      if(trees2.x <= w*0.5) then
         aux = trees1.x
         trees1.x = trees2.x
         trees2.x = trees1.x + w
      end
      if(trees1.x > w*0.5) then
         aux = trees2.x
         trees2.x = trees1.x
         trees1.x = trees2.x - w
      end
   end

   function background:init()
      -- RGB: 3 9 69

      background.thunderPng = display.newImageRect( "resources/sprites/thunder.png", X*0.20, Y*0.4 )
      background.thunderPng.y = Y*0.2
      background.thunderPng.alpha = 0

      background.trees1 = createTrees(w*0.5, Y*0.4)
      background.trees2 = createTrees(w*0.5 + w, Y*0.4)
      background.trees3 = createTrees(w*0.5, Y*0.6)
      background.trees4 = createTrees(w*0.5 + w, Y*0.6)
      background.trees5 = createTrees(w*0.5, Y*0.8)
      background.trees6 = createTrees(w*0.5 + w, Y*0.8)

      background.mountains = display.newImageRect( "resources/sprites/mountainsBG.jpg", w*3, Y*0.8 )
      background.mountains.x = w*1.3
      background.mountains.y = 0

      background.thunderLight = display.newRect( background, X*0.5, Y*0.4, X, Y*0.8 )
      background.thunderLight:setFillColor( 0, 0, 0, 0.75)

      background:insert(background.mountains)
      background:insert(background.thunderLight)
      background:insert(background.thunderPng)

      background:insert(background.trees1)
      background:insert(background.trees2)
      background:insert(background.trees3)
      background:insert(background.trees4)
      background:insert(background.trees5)
      background:insert(background.trees6)
      background:toBack()
   end

   function background:thunder()

      function transitionToDown()
         background.thunderPng.alpha = 0.0
         transition.to(background.thunderLight, {
            time = 500,
            alpha = 1.0,
            transition = easing.outBounce
         })
      end

      background.thunderPng.x = math.random(X)
      transition.to(background.thunderPng, {
         time = 300,
         alpha = 1.0
      })
      local thunderSound = audio.loadStream("resources/sounds/thunder.wav")
      audio.play(thunderSound)

      transition.to(background.thunderLight, {
         time = 500,
         alpha = 0.0,
         onComplete = transitionToDown,
         transition = easing.inBounce
      })
   end

   function background:update(delta, xAct)
      if(background.playerX == nil) then
         background.playerX = xAct
      end

      background.thunderLightTimer = background.thunderLightTimer - delta
      if(background.thunderLightTimer < 0) then
         background.thunder()
         background.thunderLightTimer = math.random(20)
      end

      local inc = -(xAct - background.playerX)
      background.playerX = xAct
      background.mountains.x = background.mountains.x + inc * 0.15

      updateTrees( background.trees1, background.trees2, inc)
      updateTrees( background.trees3, background.trees4, inc)
      updateTrees( background.trees5, background.trees6, inc)
   end

   background.init()
   return background
end

return BackgroundBG